import java.io.FileOutputStream;

 

import javax.xml.soap.MessageFactory;

import javax.xml.soap.SOAPBody;

import javax.xml.soap.SOAPBodyElement;

import javax.xml.soap.SOAPEnvelope;

import javax.xml.soap.SOAPHeader;

import javax.xml.soap.SOAPMessage;

import javax.xml.soap.SOAPPart;

public class CreateSOAPMessage {

    public static void main(String[] args) {

        try
        {
            //Initialise the message
        	MessageFactory factory = MessageFactory.newInstance();
            SOAPMessage soapMsg = factory.createMessage();
           
            //Initialise the part and assign value
            SOAPPart part = soapMsg.getSOAPPart();
            //Initialise the envelope and assign value
            SOAPEnvelope envelope = part.getEnvelope();
            //Initialise the header and assign value
            SOAPHeader header = envelope.getHeader();
            //Initialise the body and assign value
            SOAPBody body = envelope.getBody();
            
            //Add content to header
            header.addTextNode("Training Details");
            
            //Add content to body element (WS)
            SOAPBodyElement element = body.addBodyElement(envelope.createName("JAVA", "training", "http://shivasoft.in/blog"));
            element.addChildElement("WS").addTextNode("Training on Web service");
            
            //add content to body element (Spring)
            SOAPBodyElement element1 = body.addBodyElement(envelope.createName("JAVA", "training", "http://shivasoft.in/blog"));
            element1.addChildElement("Spring").addTextNode("Training on Spring 3.0");
            
            //Prints the message to screen
            soapMsg.writeTo(System.out);
            //Writes the file to file "SoapMessage.xml"
            FileOutputStream fOut = new FileOutputStream("SoapMessage.xml");
            soapMsg.writeTo(fOut);
            
            
            System.out.println();
            System.out.println("SOAP msg created");

 

        }catch(Exception e){
            e.printStackTrace();
        }

    }

}
